<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------

namespace app\common\model\area;

use app\common\model\Base;

use think\db;

class Area extends Base
{
    protected $name = 'area';
    public $ak="5vWGU9kaMUoCmaCM6MOxCARBnOXMZMM4"; //请及时更换

    public function getTree($parent_id = 0)
    {
//        $data = $this->order('sort asc')->select();
        $data = $this->getList([], 0, '*', 'first_char asc');
        return $this->_reSort($data, $parent_id);
    }

    private function _reSort($data, $parent_id = 0, $level = 0, $isClear = TRUE)
    {
        static $ret = array();
        if ($isClear)
            $ret = array();
        foreach ($data as $k => $v) {
            if ($v['pid'] == $parent_id) {
                $v['level'] = $level;
                $ret[] = $v;
                $this->_reSort($data, $v['id'], $level + 1, FALSE);
            }
        }
        return $ret;
    }

    /**
     * User:Simony
     * Function:
     * @param $id
     * @return array
     */
    public function getChildren($id)
    {
        $data = $this->select();
        return $this->_children($data, $id);
    }

    private function _children($data, $parent_id = 0, $isClear = TRUE)
    {
        static $ret = array();
        if ($isClear)
            $ret = array();
        foreach ($data as $k => $v) {
            if ($v['pid'] == $parent_id) {
                $ret[] = $v['id'];
                $this->_children($data, $v['id'], FALSE);
            }
        }
        return $ret;
    }


    /**
     * User:Simony
     * Function:获取当前城市
     * @param $latitude
     * @param $longitude
     * @return mixed
     */
    public function currentCityInfo($latitude, $longitude)
    {
        $place_url = 'http://api.map.baidu.com/geocoder/v2/?location=' . $latitude . ',' . $longitude . '&output=json&ak='.$this->ak;
        $json_place = file_get_contents($place_url);
        $place_arr = json_decode($json_place, true);
        return $place_arr['result']['addressComponent'];
    }

    public function setLatLongByName($name){
        $place_url = 'http://api.map.baidu.com/geocoder?address=' . $name .'&output=json&key='.$this->ak;
        $json_place = file_get_contents($place_url);
        $place_arr = json_decode($json_place, true);
        return $place_arr;
    }

    /**
     * User:Simony
     * Function:通过经纬度获取城市信息
     * @param $latitude
     * @param $longitude
     * @return array
     */
    public function getCityInfoByLatLong($latitude, $longitude)
    {
        $currentCityInfo = $this->currentCityInfo($latitude, $longitude);
        $res=$this->getInfo([['name', 'like', '%' . $currentCityInfo['city'] . '%']],'first_char,id,name,pid')->toArray();
        return $res ?: [];
    }
}
